#include "StdAfx.h"
#include "DrawUITree.h"
#include "DrawToolTip.h"
#include "DrawLayout.h"
namespace UILib
{
    CDrawUITree::CDrawUITree( const HWND hWnd, PUITreeObserver pObserver ) : hWnd_(hWnd)\
        , pToolTips_(NULL)\
        , pObserver_( pObserver )
    {
    }

    CDrawUITree::~CDrawUITree(void)
    {
        for ( vector<CDrawView*>::iterator it = vecElem_.begin(); it != vecElem_.end(); it++ )
        {
            CDrawView* pElem = (*it);
            if ( pElem )
            {
                delete pElem;
                pElem = NULL;
            }
        }

        vecElem_.clear();
    }

    void CDrawUITree::SetObserver( PUITreeObserver pObserver )
    {
        pObserver_ = pObserver;
    }

    void CDrawUITree::AddChildren( CDrawView* pChildren, const int nPos /*= -1 */ )
    {
        if ( nPos < 0 )
        {
            vecElem_.push_back( pChildren );
        }
        else if ( nPos < ( int )vecElem_.size() )
        {
            vecElem_.insert( vecElem_.begin() + nPos, pChildren );
        }
        pChildren->SetTipsListener(dynamic_cast<TipsListenser*>(this));
    }

    CDrawView* CDrawUITree::GetChildrenByID( const UINT uID )
    {
        CDrawView* pElement = NULL;
        for ( vector<CDrawView*>::iterator it = vecElem_.begin(); it != vecElem_.end(); it++ )
        {
            if ( uID == (*it)->GetID() )
            {
                pElement = (*it);
                break;
            }

            if ( (*it)->GetClass() == L"CDrawLayout" )
            {
                CDrawLayout* pLayout = dynamic_cast< CDrawLayout* >(*it);
                if ( pLayout )
                {
                    pElement = pLayout->GetChildrenByID(uID);
                    if ( pElement )
                    {
                        break;
                    }
                }
            }
        }
        return pElement;
    }

    int CDrawUITree::GetChildrenCount() const
    {
        return (int)vecElem_.size();
    }

    CDrawView* CDrawUITree::GetChildrenByIndex( const UINT uIndex )
    {
        if ( uIndex >= 0 && uIndex < (int)vecElem_.size() ) {
           return vecElem_[ uIndex] ;}
        else 
            return NULL;
    }

    void CDrawUITree::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam, HWND hWnd, BOOL& bHandled )
    {
        for ( vector<CDrawView*>::iterator it = vecElem_.begin(); it != vecElem_.end(); it++ )
        {
            BOOL bElemHandled = false;
            CDrawView* pElem = (*it);
            pElem->HandleMessage( uMsg, wParam, lParam, hWnd, bElemHandled );
        }
    }

    void CDrawUITree::DrawElement( const HDC hDC, const RECT &rcPaint )
    {
        for ( vector<CDrawView*>::iterator it = vecElem_.begin(); it != vecElem_.end(); it++ )
        {
            CDrawView* pElem = (*it);
            pElem->DrawElement( hDC, rcPaint );
        }
    }

    bool CDrawUITree::CheckInvalidateRect( RECT& rc )
    {
        memset(&rc, 0, sizeof(RECT) );
        bool nNeedInvalidata = false;
        for ( vector<CDrawView*>::iterator it = vecElem_.begin(); it != vecElem_.end(); it++ )
        {
            CDrawView* pElem = (*it);
            RECT rcElem = { 0 };
            if ( pElem->NeedInvalidata( rcElem ) )
            {
                nNeedInvalidata = true;
                 ::UnionRect(&rc, &rc, &rcElem);
            }
        }
        return nNeedInvalidata;
    }

    void CDrawUITree::OnShowTips( const wstring wstrTips, const POINT pt, const RECT rcElem )
    {
        //int i = 0;
        if ( !wstrTips.empty() )
        {
            if ( !pToolTips_ )
            {
                CDrawToolTip* pTips = new CDrawToolTip;
                pTips->CreateTips();
                pToolTips_ = pTips;
            }

             CDrawToolTip* pTips = (CDrawToolTip*)pToolTips_;

             POINT ptTips = { pt.x, pt.y };
             ::ClientToScreen(hWnd_, &ptTips);
            pTips->ShowTips( wstrTips, ptTips );
        }
        else
        {
            if ( pToolTips_ )
            {
                CDrawToolTip* pTips = (CDrawToolTip*)pToolTips_;
                pTips->HideTips();
            }
        }
    }

    void CDrawUITree::Layout( const RECT rcClient )
    {
        for ( vector<CDrawView*>::iterator it = vecElem_.begin(); it != vecElem_.end(); it++ )
        {
            CDrawView* pElem = (*it);
            pElem->Layout(rcClient);
        }

        if ( pObserver_ )
        {
            SIZE szWnd = { rcClient.right - rcClient.left, rcClient.bottom - rcClient.top };
            pObserver_->OnSizeChange( szWnd );
        }
    }
}
